package com.china08.yxyapi.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ThumbnailsUtil {

    /**
     * 指定宽度或高度及图片格式等比例压缩(宽高以最小值为准进行压缩)
     * 
     * @param bytesOld
     * @param widthOrHeight
     * @param formatName
     * @return
     * @throws IOException
     */
    public static byte[] perfect(byte[] bytesOld, int widthOrHeight, String formatName) throws IOException {
        if (StringUtils.isNotBlank(formatName) && formatName.trim().toLowerCase().equals("png")) {
            formatName = "png";
        } else {
            formatName = "jpeg";
        }

        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytesOld);
        BufferedImage bufferedImage = ImageIO.read(byteIn);

        boolean needProcess = true;

        // 原始宽高
        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();

        // 以宽度为准
        if (w <= h && w > widthOrHeight) {
            bufferedImage = Thumbnails.of(bufferedImage).width(widthOrHeight).outputQuality(1.0f).asBufferedImage();
        } else
        // 以高度为准
        if (h <= w && h > widthOrHeight) {
            bufferedImage = Thumbnails.of(bufferedImage).height(widthOrHeight).outputQuality(1.0f).asBufferedImage();
        } else {
            needProcess = false;
        }
        byte[] bytesNew = new byte[0];
        if (needProcess) {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            // 输出类型(格式为png,生成的图片大小较大)
            ImageIO.write(bufferedImage, formatName, byteOut);
            bytesNew = byteOut.toByteArray();
        } else {
            bytesNew = bytesOld;
        }
        return bytesNew;
    }

    /**
     * 压缩图片，
     * 已宽度为准，
     * 大于设置宽度，截取图片，小于不做处理
     * 
     * @return
     */
    public static byte[] perfectForWidth(byte[] bytesOld, int width, String formatName) throws IOException {

        if (StringUtils.isNotBlank(formatName) && formatName.trim().toLowerCase().equals("png")) {
            formatName = "png";
        } else {
            formatName = "jpeg";
        }

        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytesOld);
        BufferedImage bufferedImage = ImageIO.read(byteIn);

        // 原始宽高
        int w = bufferedImage.getWidth();

        if (w > width) {
            bufferedImage = Thumbnails.of(bufferedImage).width(width).outputQuality(1.0f).asBufferedImage();
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            // 输出类型(格式为png,生成的图片大小较大)
            ImageIO.write(bufferedImage, formatName, byteOut);
            return byteOut.toByteArray();
        }
        return bytesOld;
    }

    /**
     * 指定宽度或高度等比例压缩(宽高以最小值为准进行压缩)
     * 
     * @param bytesOld
     * @param widthOrHeight
     * @return
     * @throws IOException
     */
    public static byte[] perfect(byte[] bytesOld, int widthOrHeight) throws IOException {
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytesOld);
        BufferedImage bufferedImage = ImageIO.read(byteIn);

        boolean needProcess = true;

        // 原始宽高
        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();

        // 以宽度为准
        if (w <= h && w > widthOrHeight) {
            bufferedImage = Thumbnails.of(bufferedImage).width(widthOrHeight).outputQuality(1.0f).asBufferedImage();
        } else
        // 以高度为准
        if (h <= w && h > widthOrHeight) {
            bufferedImage = Thumbnails.of(bufferedImage).height(widthOrHeight).outputQuality(1.0f).asBufferedImage();
        } else {
            needProcess = false;
        }

        byte[] bytesNew = new byte[0];
        if (needProcess) {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            // 输出类型(格式为png,生成的图片大小较大)
            ImageIO.write(bufferedImage, "jpeg", byteOut);
            bytesNew = byteOut.toByteArray();
        } else {
            bytesNew = bytesOld;
        }
        return bytesNew;

    }

    /**
     * 正方形
     * 
     * @param bytesOld
     * @param widthOrHeight
     * @return
     * @throws IOException
     */
    public static byte[] square(byte[] bytesOld, int widthOrHeight) throws IOException {
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytesOld);
        BufferedImage bufferedImage = ImageIO.read(byteIn);
        // 以宽度为准
        bufferedImage = Thumbnails.of(bufferedImage).crop(Positions.CENTER).size(widthOrHeight, widthOrHeight).outputQuality(1.0f).asBufferedImage();
        byte[] bytesNew = new byte[0];
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        // 输出类型(格式为png,生成的图片大小较大)
        ImageIO.write(bufferedImage, "jpeg", byteOut);
        bytesNew = byteOut.toByteArray();
        return bytesNew;
    }
}
